<?php

require_once dirname(__FILE__).'/../lib/product_typeGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/product_typeGeneratorHelper.class.php';

/**
 * product_type actions.
 *
 * @package    eel
 * @subpackage product_type
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class product_typeActions extends autoProduct_typeActions
{
  public function executeNew(sfWebRequest $request)
  {
    $this->form = $this->configuration->getForm();
    $this->product_type = $this->form->getObject();
    $this->downloadTypeList = DownloadTypeTable::getDownloadTypeListOrderByName();
    $this->arr_exist_download_type = array(); 
  }
  	
  public function executeEdit(sfWebRequest $request)
  {
    $this->product_type = $this->getRoute()->getObject();
    $this->form = $this->configuration->getForm($this->product_type);
    $this->downloadTypeList = DownloadTypeTable::getDownloadTypeListOrderByName();
    $existProductTypeHasDownloadTypeList = ProductTypeHasDownloadTypeTable::getProductTypeHasDownloadTypeListByProductTypeId($this->product_type->getId());
    $arr_exist_download_type = array();
    foreach($existProductTypeHasDownloadTypeList as $item)
    {
    	$arr_exist_download_type[] = $item->getDownloadTypeId();
    }
    $this->arr_exist_download_type = $arr_exist_download_type;
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->product_type = $this->getRoute()->getObject();
    $this->form = $this->configuration->getForm($this->product_type);

    $this->processForm($request, $this->form);
    $this->setTemplate('edit');
  }	

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

      try {
        $product_type = $form->save();
      } catch (Doctrine_Validator_Exception $e) {

        $errorStack = $form->getObject()->getErrorStack();

        $message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ?  's' : null) . " with validation errors: ";
        foreach ($errorStack as $field => $errors) {
            $message .= "$field (" . implode(", ", $errors) . "), ";
        }
        $message = trim($message, ', ');

        $this->getUser()->setFlash('error', $message);
        return sfView::SUCCESS;
      }
      
	    //save the product type has download type mappings
		$old_exist_list = ProductTypeHasDownloadTypeTable::getProductTypeHasDownloadTypeListByProductTypeId($this->product_type->getId());
	    $arr_new_download_type_id = $request->getParameter('product_type_has_download_types');
		
	    if($arr_new_download_type_id != null)
		{
			$arr_old_download_type_id = array();
			foreach ($old_exist_list as $exist_one)
			{
				$arr_old_download_type_id[] = $exist_one->getDownloadTypeId();
			}
			
			$arr_new_id = array_diff($arr_new_download_type_id,$arr_old_download_type_id);
			$arr_delete_id = array_diff($arr_old_download_type_id,$arr_new_download_type_id);
			
			foreach($arr_new_id as $new_id)
			{
				$productTypeHasDownloadType = new ProductTypeHasDownloadType();
				$productTypeHasDownloadType->setProductTypeId($this->product_type->getId());
				$productTypeHasDownloadType->setDownloadTypeId($new_id);
				$productTypeHasDownloadType->save();
			}
			
			foreach($arr_delete_id as $delete_id)
			{
				foreach ($old_exist_list as $exist_one)
				{
					if($exist_one->getDownloadTypeId() == $delete_id)
					{
						$exist_one->delete();
					}
				}
			}
		} 
		else
		{
			foreach($old_exist_list as $exist_one)
			{
				$exist_one->delete();
			}
		}        

      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $product_type)));

      if ($request->hasParameter('_save_and_add'))
      {
      	
        $this->getUser()->setFlash('notice', $notice.' You can add another one below.');

        $this->redirect('@product_type_new');
      }
      else
      {
        $this->getUser()->setFlash('notice', $notice);

        $this->redirect(array('sf_route' => 'product_type_edit', 'sf_subject' => $product_type));
      }
    }
    else
    {
      $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
    }
  }  
}
